package leetcode61_70;

import org.junit.Test;

/**
 * @author shhjiang_001
 * @create 2020-06-21 12:52
 */

       /*
        给你两个二进制字符串，返回它们的和（用二进制表示）。
        输入为 非空 字符串且只包含数字 1 和 0。
        示例 1:

        输入: a = "11", b = "1"
        输出: "100"
        */

public class Solution67 {

    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for (int i = a.length()-1, j = b.length()-1 ;i >= 0 || j >= 0; i--,j--) {
            int sum = carry;
            int asum = i >= 0?a.charAt(i)-'0':0;
            int bsum = j >= 0?b.charAt(j)-'0':0;
            sum = asum+bsum+sum;
            char ch = (char) ('0'+sum%2);
            sb.append(ch);
            carry = sum/2;
        }
        if (carry==1){
            sb.append('1');
        }
        return sb.reverse().toString();
    }
    @Test
    public void test(){
        String s = addBinary("11", "1");
        System.out.println(s);
    }
}
